package com.whfc.fmam.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.enums.DelFlag;
import com.whfc.common.enums.EnableState;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.util.PageUtil;
import com.whfc.common.util.PinyinUtil;
import com.whfc.fmam.dao.FmamSupplierMapper;
import com.whfc.fmam.entity.bo.FmamSupplier;
import com.whfc.fmam.entity.dto.FmamSupplierDTO;
import com.whfc.fmam.entity.qo.SupplierAddQO;
import com.whfc.fmam.service.FmamSupplierService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@DubboService(interfaceClass = FmamSupplierService.class, version = "1.0.0")
public class FmamSupplierServiceImpl implements FmamSupplierService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FmamSupplierMapper fmamSupplierMapper;

    @Override
    public PageData<FmamSupplierDTO> listFmamSupplierDTO(Integer pageNum, Integer pageSize, Integer category, Integer deptId) throws BizException {
        logger.info("查询物资合作单位列表服务，pageNum:{},pageSize:{},deptIds:{}", pageNum, pageSize, deptId);
        PageHelper.startPage(pageNum, pageSize);
        List<FmamSupplierDTO> list = fmamSupplierMapper.selectFmamSupplierByCategoryAndDeptId(category, deptId);
        PageHelper.clearPage();
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public ListData<FmamSupplierDTO> list(Integer deptId, Integer category) {
        logger.info("获取所有的收发料单位服务，deptId:{},category:{}", deptId, category);
        List<FmamSupplierDTO> list = fmamSupplierMapper.selectFmamSupplierByCategoryAndDeptId(category, deptId);
        ListData<FmamSupplierDTO> listData = new ListData<>(list);
        return listData;
    }

    @Override
    public List<FmamSupplierDTO> listFmamSupplierDTO(Integer deptId) throws BizException {
        List<FmamSupplierDTO> list = fmamSupplierMapper.selectByDeptId(deptId);
        for (FmamSupplierDTO fmamSupplierDTO : list) {
            if (!StringUtils.isEmpty(fmamSupplierDTO.getEname())) {
                fmamSupplierDTO.setEname(fmamSupplierDTO.getEname().substring(0, 1));
            }
        }
        return list;
    }

    @Override
    public FmamSupplierDTO getFmamSupplierDetail(Integer id) throws BizException {
        logger.info("查询物资合作单位详情服务，id:{}", id);

        FmamSupplier fmamSupplier = fmamSupplierMapper.selectByPrimaryKey(id);
        FmamSupplierDTO fmamSupplierDTO = new FmamSupplierDTO();
        BeanUtils.copyProperties(fmamSupplier, fmamSupplierDTO);
        return fmamSupplierDTO;
    }

    @Override
    public void delSupplier(Integer id) throws BizException {
        logger.info("逻辑移除物资合作单位服务，id:{}", id);
        fmamSupplierMapper.deleteLogicById(id);
    }

    @Override
    public FmamSupplierDTO getSupplierByCorpIdAndCategory(Integer corpId, Integer category) throws BizException {
        return fmamSupplierMapper.selectSupplierByCorpIdAndCategory(corpId, category);
    }

    @Override
    public void addSupplier(SupplierAddQO supplierAddQO) throws BizException {
        logger.info("新增物资合作单位服务，supplierAddQO:{}", supplierAddQO.toString());
        FmamSupplier fmamSupplier = new FmamSupplier();
        BeanUtils.copyProperties(supplierAddQO, fmamSupplier);
        fmamSupplier.setEname(PinyinUtil.toPinyinAbridge(supplierAddQO.getName()));
        //默认启用状态
        fmamSupplier.setEnableFlag(EnableState.ENABLED.getValue());
        fmamSupplier.setDelFlag(DelFlag.UNDELETE.getValue());
        //设置名称字母对应
        fmamSupplier.setEname(PinyinUtil.toPinyinAbridge(supplierAddQO.getName()));
        fmamSupplierMapper.insert(fmamSupplier);
    }

    @Override
    public List<Integer> getCorpIds(Integer category, Integer deptId) throws BizException {
        List<Integer> corpIds = new ArrayList<>();
        List<FmamSupplierDTO> list = fmamSupplierMapper.selectFmamSupplierByCategoryAndDeptId(category, deptId);
        if (list != null && list.size() > 0) {
            //循环遍历添加合作单位主键ID到集合
            for (FmamSupplierDTO fmamSupplierDTO : list) {
                Integer corpId = fmamSupplierDTO.getCorpId();
                corpIds.add(corpId);
            }
        }
        return corpIds;
    }

    @Override
    public void deleteBatch(List<Integer> idList) {
        if(CollectionUtils.isEmpty(idList)){
            return;
        }
        fmamSupplierMapper.deleteBatch(idList);
    }


}
